package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.Admin;
import com.hqyj.pojo.User;
import com.hqyj.dao.UserMapper;
import com.hqyj.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class UserServiceImpl implements UserService {
    //注入mapper
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public HashMap<String, Object> select(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<User> page = new Page<>(user.getPage(),user.getRow());

        //条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(user.getUserId()!=null,"user_id",user.getUserId());
        queryWrapper.like(user.getUserName()!=null && !user.getUserName().equals(""),"user_name",user.getUserName());

        //查询数据库
        Page<User> aList =userMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",aList.getRecords());
        //总页数
        map.put("totalPage",aList.getPages());
        //总条数
        map.put("total",aList.getTotal());
        //当前页
        map.put("curPage",user.getPage());
        if(user.getPage()==1){
            //上一页
            map.put("prePage",1);
        }else{
            map.put("prePage",user.getPage()-1);
        }
        if(user.getPage()==aList.getPages()){
            //下一页
            map.put("nextPage",aList.getPages());
        }else{
            map.put("nextPage",user.getPage()+1);
        }
        //每页显示条数
        map.put("row",user.getRow());
        return map;
    }

    //添加功能
    @Override
    public HashMap<String, Object> add(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = userMapper.insert(user);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override//修改功能
    public HashMap<String, Object> update(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //修改
        int num = userMapper.updateById(user);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override//删除功能
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num = userMapper.deleteById(id);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> selectById(Integer id) {
        HashMap<String,Object> map=new HashMap<String,Object>();
        User user=userMapper.selectById(id);
        map.put("user",user);
        return map;
    }

    @Override
    public HashMap<String, String> login(User user) {
        HashMap<String, String> map = new HashMap<String, String>();
        try {
            //1 用户令牌(用户和密码对象)
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPassWord());
            //2 创建shiro的用户对象
            Subject su = SecurityUtils.getSubject();
            //3 登录
            su.login(token);
            map.put("info", "登录成功");
        } catch (UnknownAccountException u) {
            u.printStackTrace();
            map.put("info", "用户名不正确");
            System.out.println("用户名不正确");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            map.put("info", "密码不正确");
            System.out.println("密码不正确");
        }
        return map;
    }
}
